<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\facade\Cache;

class AuthGroupModel extends Model
{
    const STATUS_SHOW=1; //显示
    const STATUS_HIDE=0; //不显示

    const TYPE_ID=0;

    protected $autoWriteTimestamp = true;

    /**
     * 所有分组
     * @param int $pageSum 每页显示条数
     * @param bool $status 是否需要分页
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getALL($pageSum=0,$status=false)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        if(!$status){
            $list=self::where('type_id',self::TYPE_ID)->paginate($pageSum);
        }else{
            $list=self::where('type_id',self::TYPE_ID)
                ->where("status",self::STATUS_SHOW)
                ->select();
        }

        return $list;
    }



    /**
     * 增加分组
     * @param $data
     * @return bool
     */
    public function addGroup($data){
        //检测当前应用方法名是否重复
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'分组已存在！'];
        }

        if(empty($data['id'])){
            $status = self::save($data);

            if($status){
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $status = self::save($data,['id'=>$data["id"]]);

            if($status){
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $status=self::destroy($id);
        if($status){

            //TODO 删除所分配的权限

            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['id'])){
            $data['id']=0;
        }

        $count=self::where('title',$data['title'])
            ->where('type_id',self::TYPE_ID)
            ->where('id',"<>",$data['id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 后台权限检测
     * @param $gid
     * @param $req
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function check_auth($gid,$req){
        $info=self::field("auth_list")->get($gid);

        if(empty($info) || empty($info["auth_list"]) || $info["auth_list"]==""){
            return false;
        }

        $am=AdminMenuModel::field('id')->where('app',$req["app"])
            ->where('controller',$req["controller"])
            ->where('action',$req['action'])
            ->find();
        if (empty($am)){
            return false;
        }

        if(!in_array($am['id'],explode(",",$info['auth_list']))){
            return false;
        }

        return true;
    }

}